/**
 * 
 */
package com.elite.groovy.callback


import com.elite.groovy.util.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.elite.groovy.util.Util;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
/**
 * 
 * @author luper
 *
 */

public  class YLTstatusService implements ServiceBackInvoke{
    public static final Log log = LogFactory.getLog(YLTstatusService.class);
public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response){
    log.debug("-----Begin Systeam----------");
    response.setHeader("Content-type", "text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");

    Util u = new Util();
    String requestJSONStr =u.getRequestBody(request, "UTF-8");
    log.debug("requestJSONStr:" + requestJSONStr);
    request.getParameter("interfacetype");
	
    //获取接口请求类型：status 一路通状态回传/images 图片回传
    String v_interfacetype=request.getParameter("interfacetype");
    log.debug("v_interfacetype:"+v_interfacetype);
	
	//报文解析(通用字段)
	//非车险道路救援
	String v_orderId;
	JSONObject json  = new JSONObject().fromObject(requestJSONStr);
	if(v_interfacetype=='djstatus' || v_interfacetype=='scstatus' || v_interfacetype=='yistatus'){//代驾服务、安全检测、年检					
		v_orderId = json.getString("thirdOrderNo");    		 //订单ID
		log.debug("v_orderId:"+v_orderId);
	}else{			//救援项目
		v_orderId= json.getString("ticketId");    		 //订单ID
		log.debug("v_orderId:"+v_orderId);

	}


    try{

        //数据校验
        def querysql1="select count(*) count1 from objective where workorderid='"+v_orderId+"'";
        def count1="";
        def list1=eliteBean.chaxun_sql(querysql1,"dataSource");
        log.debug("list-------------------------------"+list1);
        for(def k=0;k<list1.size;k++){
            def map1=list1[k];
            def v_count1=map1.get("count1");
            log.debug("v_count1---v_count1-----v_count1----"+v_count1);

            if(v_count1>0){
                //吉诺救援状态回传业务逻辑
                if(v_interfacetype=='status'){	
                    String v_orderNumber = json.getString("yltOrderNo");    //订单编号
                    String v_status = json.getString("statusCode");				 //本次回调的状态
                    String v_time = json.getString("statusTime");                  //时间戳
					def jsonstring=json.toString();
                    def orderRemark;
					if(jsonstring.indexOf("remark") > 0){
						orderRemark = json.getString("remark");  				//备注
					}

                    if	(v_status != null) {	
                        if (v_status=="2"){
                            v_status = "调度";}
                        else if (v_status == "3"){
                            v_status =  "接单";}
                        else if (v_status == "4"){
                            v_status =  "到达";}
                        else if (v_status == "5"){
                            v_status =  "完成";}
                        else if (v_status == "8"){
                            v_status =  "取消";}
                        else if (v_status == "10"){
                            v_status =  "空驶";
                        }
                        //服务商操作轨迹记录	 
                        def updatesql1="insert into workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID,OLDHANDLEBY_ID, OLDHANDLEGROUP_ID,WORKORDERSTEPSTATUS,COMMENTS,MODIFIEDDATE )values((select objective_guid from objective where workorderid='"+v_orderId+"'),sysguid,'000143','16J64F','FWSHD','"+v_status+"',sysdate)";
                        def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");		
                        log.debug("updatesqlresult1:"+updatesqlresult1);			
						eliteBean.update("update tabcdh_vas_list set DD_CASEID='"+v_orderNumber+"' where order_id='"+v_orderId+"'","dataSource");
                        //更新订单状态
                        if(v_status=="完成"){
                            eliteBean.update("update tabcdh_vas_list set orderstatus='1'  where order_id='"+v_orderId+"'","dataSource");
                        }			
                        if(v_status=="取消" || v_status=="空驶"){	

                            //更新tabc取消信息字段
                            def updatesql2="update tabcdh_vas_list set CANCELER='壹路通服务商',CANCEL_TIME='"+v_time+"',CANCEL_FLAG=1,DD_CASEID='"+v_orderNumber+"',orderstatus='3',order_remark='"+orderRemark+"',cancel_reason='"+orderRemark+"' where order_id='"+v_orderId+"'";
                            def updatesqlresult2=eliteBean.update(updatesql2,"dataSource");
                            log.debug("updatesqlresult2:"+updatesqlresult2);
                            //更新obj
                            def updatesql3="update objective set OBJECTIVESTATUS='CLOSE' where workorderid='"+v_orderId+"'";
                            def updatesqlresult3=eliteBean.update(updatesql3,"dataSource");
                            log.debug("updatesqlresult3:"+updatesqlresult3);
                            //删除elitetask
                            def updatesql4="delete from elitetask where objective_guid in (select objective_guid from objective where workorderid='"+v_orderId+"')";
                            def updatesqlresult4=eliteBean.update(updatesql4,"dataSource");
                            log.debug("updatesqlresult4:"+updatesqlresult4);				
                        }

                        //返回成功结果
                        JSONObject  jsonALL3 = new JSONObject();
                        jsonALL3.put("flag", "1");
                        jsonALL3.put("error", "");
                        return jsonALL3;
                    }
                }
					//代驾状态回传
				if(v_interfacetype=='djstatus'){	
					log.debug("-----json----------"+json);
                    def v_status = json.getString("orderStatus");				 //本次回调的状态
                    def v_time = json.getString("updateTime");                  //时间戳
					def jsonstring=json.toString();
					def orderRemark;
					if(jsonstring.indexOf("remark") > 0){
						orderRemark = json.getString("remark");  				//备注
					}
				
                    if	(v_status != null) {	
                        if (v_status=="2"){
                            v_status = "已接单";}
                        else if (v_status == "3"){
                            v_status =  "已到达";}
                        else if (v_status == "4"){
                            v_status =  "已出发";}
                        else if (v_status == "5"){
                            v_status =  "已完成";}
                        else if (v_status == "6"){
                            v_status =  "已取消";}

                        //服务商操作轨迹记录	 
                        def updatesql1="insert into workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID,OLDHANDLEBY_ID, OLDHANDLEGROUP_ID,WORKORDERSTEPSTATUS,COMMENTS,MODIFIEDDATE )values((select objective_guid from objective where workorderid='"+v_orderId+"'),sysguid,'000143','16J64F','FWSHD','"+v_status+"',sysdate)";
                        def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");		
                        log.debug("updatesqlresult1:"+updatesqlresult1);			
                        //更新订单状态
                        if(v_status=="已完成"){
                            eliteBean.update("update tabcdh_vas_list set orderstatus='1'  where order_id='"+v_orderId+"'","dataSource");
                        }			
                        if(v_status=="已取消"){	

                            //更新tabc取消信息字段
                            def updatesql2="update tabcdh_vas_list set CANCELER='壹路通代驾服务商',CANCEL_TIME='"+v_time+"',CANCEL_FLAG=1,orderstatus='3',order_remark='"+orderRemark+"',cancel_reason='"+orderRemark+"' where order_id='"+v_orderId+"'";
                            def updatesqlresult2=eliteBean.update(updatesql2,"dataSource");
                            log.debug("updatesqlresult2:"+updatesqlresult2);
                            //更新obj
                            def updatesql3="update objective set OBJECTIVESTATUS='CLOSE' where workorderid='"+v_orderId+"'";
                            def updatesqlresult3=eliteBean.update(updatesql3,"dataSource");
                            log.debug("updatesqlresult3:"+updatesqlresult3);
                            //删除elitetask
                            def updatesql4="delete from elitetask where objective_guid in (select objective_guid from objective where workorderid='"+v_orderId+"')";
                            def updatesqlresult4=eliteBean.update(updatesql4,"dataSource");
                            log.debug("updatesqlresult4:"+updatesqlresult4);				
                        }

                        //返回成功结果
                        JSONObject  jsonALL3 = new JSONObject();
                        jsonALL3.put("code", "S000000");
						jsonALL3.put("message", "");
                        jsonALL3.put("data", "");
                        return jsonALL3;
                    }
                }

					//安全检测
				if(v_interfacetype=='scstatus'){	
					log.debug("-----json----------"+json);
                    def v_status = json.getString("orderStatus");				 //本次回调的状态
                    def v_time = json.getString("updateTime");                  //时间戳
					def jsonstring=json.toString();
					def orderRemark;
					if(jsonstring.indexOf("remark") > 0){
						orderRemark = json.getString("remark");  				//备注
					}
				
                    if	(v_status != null) {	
                        if (v_status=="3"){
                            v_status = "已到店";
							}
                        else if (v_status == "5"){
                            v_status =  "已完成";
							}
                        else if (v_status == "6"){
                            v_status =  "已取消";
							}                     

                        //服务商操作轨迹记录	 
                        def updatesql1="insert into workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID,OLDHANDLEBY_ID, OLDHANDLEGROUP_ID,WORKORDERSTEPSTATUS,COMMENTS,MODIFIEDDATE )values((select objective_guid from objective where workorderid='"+v_orderId+"'),sysguid,'000143','16J64F','FWSHD','"+v_status+"',sysdate)";
                        def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");		
                        log.debug("updatesqlresult1:"+updatesqlresult1);			
                        //更新订单状态
                        if(v_status=="已完成"){
                            eliteBean.update("update tabcdh_vas_list set orderstatus='1'  where order_id='"+v_orderId+"'","dataSource");
                        }			
                        if(v_status=="已取消"){	

                            //更新tabc取消信息字段
                            def updatesql2="update tabcdh_vas_list set CANCELER='壹路通服务商',CANCEL_TIME='"+v_time+"',CANCEL_FLAG=1,orderstatus='3',order_remark='"+orderRemark+"',cancel_reason='"+orderRemark+"' where order_id='"+v_orderId+"'";
                            def updatesqlresult2=eliteBean.update(updatesql2,"dataSource");
                            log.debug("updatesqlresult2:"+updatesqlresult2);
                            //更新obj
                            def updatesql3="update objective set OBJECTIVESTATUS='CLOSE' where workorderid='"+v_orderId+"'";
                            def updatesqlresult3=eliteBean.update(updatesql3,"dataSource");
                            log.debug("updatesqlresult3:"+updatesqlresult3);
                            //删除elitetask
                            def updatesql4="delete from elitetask where objective_guid in (select objective_guid from objective where workorderid='"+v_orderId+"')";
                            def updatesqlresult4=eliteBean.update(updatesql4,"dataSource");
                            log.debug("updatesqlresult4:"+updatesqlresult4);				
                        }

                        //返回成功结果
                        JSONObject  jsonALL3 = new JSONObject();
                        jsonALL3.put("code", "S000000");
						jsonALL3.put("message", "");
                        jsonALL3.put("data", "");
                        return jsonALL3;
                    }
                }
				
				//年检
				if(v_interfacetype=='yistatus'){	
					log.debug("-----json----------"+json);
                    def v_status = json.getString("orderStatus");				 //本次回调的状态
                    def v_time = json.getString("updateTime");                  //时间戳
					def jsonstring=json.toString();
					def orderRemark;
					if(jsonstring.indexOf("remark") > 0){
						orderRemark = json.getString("remark");  				//备注
					}
				
                    if	(v_status != null) {	
                        if (v_status=="1"){
                            v_status = "已确认";
						}else if (v_status == "3"){
                            v_status =  "处理中";
						}else if (v_status == "4"){
                            v_status =  "办理成功";
						}else if (v_status == "5"){
                            v_status =  "已完成";
						}else if (v_status == "6"){
                            v_status =  "已取消";
						}else if (v_status == "9"){
                            v_status =  "待补交";
						}else if (v_status == "11"){
                            v_status =  "已接单";
						}else if (v_status == "13"){
                            v_status =  "取车成功";
						}else if (v_status == "14"){
                            v_status =  "检测中";
						}else if (v_status == "15"){
                            v_status =  "检测完成";
						}else if (v_status == "16"){
                            v_status =  "车辆归还中";
						}else if (v_status == "17"){
                            v_status =  "办理失败";
						}else if (v_status == "18"){
                            v_status =  "到场未服务";
						}                    

                        //服务商操作轨迹记录	 
                        def updatesql1="insert into workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID,OLDHANDLEBY_ID, OLDHANDLEGROUP_ID,WORKORDERSTEPSTATUS,COMMENTS,MODIFIEDDATE )values((select objective_guid from objective where workorderid='"+v_orderId+"'),sysguid,'000143','16J64F','FWSHD','"+v_status+"',sysdate)";
                        def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");		
                        log.debug("updatesqlresult1:"+updatesqlresult1);			
                        //更新订单状态
                        if(v_status=="已完成"){
                            eliteBean.update("update tabcdh_vas_list set orderstatus='1'  where order_id='"+v_orderId+"'","dataSource");
                        }			
                        if(v_status=="已取消"){	

                            //更新tabc取消信息字段
                            def updatesql2="update tabcdh_vas_list set CANCELER='壹路通服务商',CANCEL_TIME='"+v_time+"',CANCEL_FLAG=1,orderstatus='3',order_remark='"+orderRemark+"',cancel_reason='"+orderRemark+"' where order_id='"+v_orderId+"'";
                            def updatesqlresult2=eliteBean.update(updatesql2,"dataSource");
                            log.debug("updatesqlresult2:"+updatesqlresult2);
                            //更新obj
                            def updatesql3="update objective set OBJECTIVESTATUS='CLOSE' where workorderid='"+v_orderId+"'";
                            def updatesqlresult3=eliteBean.update(updatesql3,"dataSource");
                            log.debug("updatesqlresult3:"+updatesqlresult3);
                            //删除elitetask
                            def updatesql4="delete from elitetask where objective_guid in (select objective_guid from objective where workorderid='"+v_orderId+"')";
                            def updatesqlresult4=eliteBean.update(updatesql4,"dataSource");
                            log.debug("updatesqlresult4:"+updatesqlresult4);				
                        }

                        //返回成功结果
                        JSONObject  jsonALL3 = new JSONObject();
                        jsonALL3.put("code", "S000000");
						jsonALL3.put("message", "");
                        jsonALL3.put("data", "");
                        return jsonALL3;
                    }
                }

                //吉诺照片信息回传业务逻辑
                if(v_interfacetype=='images'){                   
					JSONArray LIST1 = json.getJSONArray("imageList");				//本次回调的照片						
						LIST1.each{ 
					def v_fileType=it.imageUrl;                       //文件类型
					log.debug("v_fileType:"+v_fileType); 
					
                    //更新数据	
                    def updatesql1="insert into uploadlist (UPLOADGUID, CREATEDATE, PATH ,FILENAME ,STAFF_ID, GUID,CONFIDENTIAL_TYPE ,UPLOAD_STATUS) values(sysguid,sysdate,'"+v_fileType+"','','000143',(select objective_guid from objective where workorderid='"+v_orderId+"'),'1','0')";
                    def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");
                    log.debug("updatesqlresult1:"+updatesqlresult1);
					}
                    //返回成功结果
                    JSONObject  jsonALL3 = new JSONObject();
                    jsonALL3.put("flag", "1");
                    jsonALL3.put("error", "");
                    return jsonALL3;
                }

                //吉诺GPS信息回传业务逻辑
                if(v_interfacetype=='GPS'){	 
                    JSONArray LIST1 = json.getJSONArray("rescuePointList");
                    log.debug("GPS路径信息列表:"+LIST1);	
                    LIST1.each{ 
                        def v_longitude=it.longitude;                     //经度
                        def v_latitude=it.latitude;                       //纬度			
                        def v_loc_time=it.sendTime;                       //时间			

                        //更新数据	
                        def updatesql1="update tabcdh_vas_list set jygps_lat='"+v_latitude+"',jygps_lon='"+v_longitude+"',jsgps_modifydate='"+v_loc_time+"' where ORDER_ID='"+v_orderId+"'";
                        def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");
                        log.debug("updatesqlresult1:"+updatesqlresult1);
                    }	
                    //返回成功结果
                    JSONObject  jsonALL3 = new JSONObject();
                    jsonALL3.put("flag", "1");
                    jsonALL3.put("error", "");
                    return jsonALL3;
                }
				
				//核算金额
				if(v_interfacetype=='amount'){	 
			
					def aRescueAmount=json.getString("chargeAmt");
					log.debug("--------aRescueAmount----------------:"+aRescueAmount);
					eliteBean.update("update tabcdh_vas_list set CASE_COST='"+aRescueAmount+"',help_result='完成' where order_id='"+v_orderId+"'","dataSource");
					//返回成功结果
					JSONObject  jsonALL3 = new JSONObject();
					jsonALL3.put("flag", "1");
					jsonALL3.put("error", "");
					return jsonALL3;
				}
			}else{
                    JSONObject  jsonALL3 = new JSONObject();
                    jsonALL3.put("flag", "0");
                    jsonALL3.put("error", "未找到工单号");
                    return jsonALL3;

                }
            }
        
    }catch(Exception e){
        log.debug("--------Exception----------------:"+e);
        JSONObject  jsonALL3 = new JSONObject();
		if(v_interfacetype=='djstatus' || v_interfacetype=='scstatus' || v_interfacetype=='yistatus'){
			jsonALL3.put("code", "E000004");
			jsonALL3.put("message", "json解析失败:"+e);
			jsonALL3.put("data", "");
		}else{
			jsonALL3.put("flag", "0");
			jsonALL3.put("error", "json解析失败"+e);		
		}
        return jsonALL3;
    }
}			
}

